#!/usr/bin/env bash

#docker目录
DOCKER_DIR=/root/ctc-docker

#ctc目录
CTC_DIR=${DOCKER_DIR}/html/ctc

#config.php
ctc_config_php=${CTC_DIR}/config/config.php

#静态文件版本号
static_version=$(date +%s)

#成功信息输出
success_print() {
  echo -e "\033[32m $1 \033[0m"
}

#失败信息输出
error_print() {
  echo -e "\033[31m $1 \033[0m"
}

if [ ! -d "${CTC_DIR}" ]; then
  error_print "\n------ ctc dir not existed ------\n"
fi

#拉取docker更新
cd ${DOCKER_DIR} && git pull --no-rebase

#拉取ctc源码更新
pull_result=$(cd "${CTC_DIR}" && git pull --no-rebase)

#更新静态资源版本号
sed -i "s/\$config\['static_version'\].*/\$config['static_version'] = '${static_version}';/g" "${ctc_config_php}"

docker exec -i ctc-php bash <<'EOF'

#切换到ctc目录
cd /var/www/html/ctc || exit

#安装依赖包
composer install --no-dev
composer dump-autoload --optimize

#数据迁移
vendor/bin/phinx migrate

#程序升级
php console.php upgrade

exit
EOF

success_print "\n------ upgrade finished -------\n"
